using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Windows.Markup;
using System.Xml;

[assembly: AssemblyTitle("Convert arbitary Xaml to a DrawingGroup")]
[assembly: AssemblyProduct("XamlToys")]
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyCompany("BCDev")]
[assembly: AssemblyCopyright("© 2007 Blake Coverett")]
[assembly: AssemblyDescription("Distribute freely under the Ms-PL.")]

namespace BCDev.XamlToys {
	class Xaml2Drawing {
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        [STAThread]
		static int Main(string[] args) {
			if (args.Length != 2) Fail("Usage: xaml2drawing <xaml-input-file> <xaml-output-file>");

            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
            try {
				var drawing = Utility.GetDrawingFromXaml(Utility.LoadXamlFromFile(args[0]));
				if (drawing == null) Fail("No Drawing found in Xaml.");

				Utility.MakeDrawingSerializable(drawing);

				using (var xw = XmlWriter.Create(args[1], new XmlWriterSettings {CloseOutput = true, Indent = true}))
					XamlWriter.Save(drawing, xw);

			} catch (Exception e) {
				Fail(e.Message);
			}

            DebugPause();
            return 0;
		}

        static void Fail(string message)
        {
            Console.Error.WriteLine(message);
            DebugPause();
            Environment.Exit(1);
        }

        [Conditional("DEBUG")]
        static void DebugPause()
        {
            Console.Write("\r\nPress any key to exit...");
            Console.ReadKey();
        }
    }
}
